//
//  SearchUserTableViewCell.m
//  项目三
//
//  Created by rimi1 on 2016/12/14.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "SearchUserTableViewCell.h"

static NSInteger spacing_ = 10;

@implementation SearchUserTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.headImage];
        [self.contentView addSubview:self.name];
        [self.contentView addSubview:self.words];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect nameRect = self.name.frame;
    nameRect.size.width = [SearchUserTableViewCell widthWithText:self.name.text];
    self.name.frame = nameRect;
    
    CGRect rect = self.words.frame;
    CGFloat width = [SearchUserTableViewCell widthWithText:self.words.text];
    rect.size.width = width;
    rect.origin.x = kWIDTH - spacing_ - width;
    self.words.frame = rect;
    
}

+ (CGFloat)widthWithText:(NSString *)text {
    CGFloat width  = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 15) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.width;
    
    return width;
}

#pragma mark - get

- (UIImageView *)headImage {
    
    if (!_headImage) {
        
        CGFloat diameter = 30;
        _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(spacing_, spacing_/5, diameter, diameter)];
        _headImage.layer.cornerRadius = diameter/2;
        _headImage.layer.masksToBounds = YES;
        
    }
    return _headImage;
}

- (UILabel *)name {
    
    if (!_name) {
        
        _name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.headImage.frame) + spacing_, CGRectGetMinY(self.headImage.frame), 0, CGRectGetHeight(self.headImage.bounds))];
        _name.font = [UIFont systemFontOfSize:15];
        
    }
    return _name;
}

- (UILabel *)words {
    
    if (!_words) {
        
        _words = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(self.headImage.frame), 0, CGRectGetHeight(self.headImage.bounds))];
        _words.font = [UIFont systemFontOfSize:15];
        _words.textColor = [UIColor lightGrayColor];
        
    }
    return _words;
}


@end











